package org.evej.apiparser.parser.handler.character;

import org.evej.apiparser.model.character.ApiCharacterSheet;
import org.evej.apiparser.model.character.ApiCoorporationRole;
import org.evej.apiparser.model.character.ApiSkill;

import org.evej.apiparser.parser.handler.ApiDocumentHandler;

import org.evej.model.Augmentation;
import org.evej.model.SkillAttribute;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class CharacterSheetHandler extends ApiDocumentHandler<ApiCharacterSheet> {
    private ApiCharacterSheet.Builder characterSheetBuilder;
    private Augmentation.Builder augmentationBuilder;
    private String currentRowset;

    @Override
    public void startDocument() throws SAXException {
        characterSheetBuilder = new ApiCharacterSheet.Builder();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        switch (qName) {
            case "intelligenceBonus":
                augmentationBuilder = new Augmentation.Builder(SkillAttribute.INTELLIGENCE);
                break;
            case "memoryBonus":
                augmentationBuilder = new Augmentation.Builder(SkillAttribute.MEMORY);
                break;
            case "charismaBonus":
                augmentationBuilder = new Augmentation.Builder(SkillAttribute.CHARISMA);
                break;
            case "perceptionBonus":
                augmentationBuilder = new Augmentation.Builder(SkillAttribute.PERCEPTION);
                break;
            case "willpowerBonus":
                augmentationBuilder = new Augmentation.Builder(SkillAttribute.WILLPOWER);
                break;
            case "rowset":
                currentRowset = attributeToString(attrs, "name");
                break;
            case "row": {
                switch (currentRowset) {
                    case "skills" :
                        characterSheetBuilder.addSkill(new ApiSkill(attributeToInt(attrs, "typeID"), attributeToInt(attrs, "level"),
                                attributeToInt(attrs, "skillpoints")));
                        break;
                    case "certificates" :
                        characterSheetBuilder.addCertificate(attributeToInt(attrs, "certificateID"));
                        break;
                    case "corporationRoles" :
                        characterSheetBuilder.addCoorporationRole(
                                new ApiCoorporationRole(attributeToInt(attrs, "roleID"), attributeToString(attrs, "roleName")));
                        break;
                    case "corporationRolesAtHQ" :
                        characterSheetBuilder.addCoorporationRoleAtHQ(
                                new ApiCoorporationRole(attributeToInt(attrs, "roleID"), attributeToString(attrs, "roleName")));
                        break;
                    case "corporationRolesAtBase" :
                        characterSheetBuilder.addCoorporationRoleAtBase(
                                new ApiCoorporationRole(attributeToInt(attrs, "roleID"), attributeToString(attrs, "roleName")));
                        break;
                    case "corporationRolesAtOther" :
                        characterSheetBuilder.addCoorporationRoleAtOther(
                                new ApiCoorporationRole(attributeToInt(attrs, "roleID"), attributeToString(attrs, "roleName")));
                        break;
                    case "corporationTitles" :
                        characterSheetBuilder.addCoorporationTitle(attributeToInt(attrs, "roleID"), attributeToString(attrs, "roleName"));
                        break;
                }
                break;
            }
        }
        super.startElement(uri, localName, qName, attrs);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        switch (qName) {
            case "characterID":
                characterSheetBuilder.setCharacterID(elementContentToLong());
                break;
            case "name":
                characterSheetBuilder.setName(elementContentToString());
                break;
            case "race":
                characterSheetBuilder.setRace(elementContentToString());
                break;
            case "DoB":
                characterSheetBuilder.setDateOfBirth(getDate());
                break;
            case "bloodLine":
                characterSheetBuilder.setBloodLine(elementContentToString());
                break;
            case "ancestry":
                characterSheetBuilder.setAncestry(elementContentToString());
                break;
            case "gender":
                characterSheetBuilder.setGender(elementContentToString());
                break;
            case "corporationID":
                characterSheetBuilder.setCorporationID(elementContentToLong());
                break;
            case "corporationName":
                characterSheetBuilder.setCorporationName(elementContentToString());
                break;
            case "balance":
                characterSheetBuilder.setBalance(elementContentToDouble());
                break;
            case "augmentatorName":
                augmentationBuilder.setName(elementContentToString());
                break;
            case "augmentatorValue":
                augmentationBuilder.setBonus(elementContentToInt());
                break;
            case "intelligence":
                characterSheetBuilder.setIntelligence(elementContentToInt());
                break;
            case "memory":
                characterSheetBuilder.setMemory(elementContentToInt());
                break;
            case "charisma":
                characterSheetBuilder.setCharisma(elementContentToInt());
                break;
            case "perception":
                characterSheetBuilder.setPerception(elementContentToInt());
                break;
            case "willpower":
                characterSheetBuilder.setWillpower(elementContentToInt());
                break;
        }
        if (qName.endsWith("Bonus")) {
            characterSheetBuilder.addAugmentation(augmentationBuilder.build());
            augmentationBuilder = null;
        }
        super.endElement(uri, localName, qName);
    }

    @Override
    public void endDocument() throws SAXException {
        super.documentModel = characterSheetBuilder.build();
        super.endDocument();
    }

}
